package ru.spb.osll.myfit.server.parser;

import java.util.HashMap;
import java.util.Set;

import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableStyle;
import ru.spb.osll.myfit.fixtures.base.MyFitTableFixture;
import ru.spb.osll.myfit.fixtures.base.OneLineFixture;
import ru.spb.osll.myfit.util.ReflectionUtils;
import fit.ColumnFixture;
import fit.RowFixture;
import fitlibrary.DoFixture;
import fitlibrary.SetUpFixture;

public class TableUtils {
 private HashMap<String, Class<?>> m_fixtureClasses;
  
  public TableUtils(HashMap<String, Class<?>> map){
    m_fixtureClasses = new HashMap<String, Class<?>>(map);
  }
  
  public TableStyle getTableStyle(Table table){
    String tableName = table.getData().get(0).get(0).getText();
    return getTableStyle(m_fixtureClasses.get(tableName));
  }
  
  public static TableStyle getTableStyle(Class< ? > clazz) {
    if (clazz == null) {
      return TableStyle.UNKNOWN_FIXTURE;
    }

    Set<String> hierarhy = ReflectionUtils.getClassHierarhy(clazz);

    if (hierarhy.contains(SetUpFixture.class.getCanonicalName())) {
      return TableStyle.SETUP_FIXTURE;
    } else if (hierarhy.contains(DoFixture.class.getCanonicalName())) {
      return TableStyle.DO_FIXTURE;
    } else if (hierarhy.contains(OneLineFixture.class.getCanonicalName())) {
      return TableStyle.ONE_LINE_FIXTIRE;
    } else if (hierarhy.contains(RowFixture.class.getCanonicalName())) {
      return TableStyle.ROW_FIXTURE;
    } else if (hierarhy.contains(MyFitTableFixture.class.getCanonicalName())) {
      return TableStyle.MYFIT_TABLE_FIXTURE;
    } else if (hierarhy.contains(ColumnFixture.class.getCanonicalName())) {
      return TableStyle.COLUMN_FIXTURE;
    } else {
      return TableStyle.UNKNOWN_FIXTURE;
    }
  }
}
